package com.ruoyi.web.controller.byhealth;


import com.ruoyi.by.health.domain.entity.ByhealthEmployee;
import com.ruoyi.by.health.domain.entity.ByhealthLotteryOutcome;
import com.ruoyi.by.health.domain.entity.ByhealthPrize;
import com.ruoyi.by.health.domain.vo.LotteryOutcomeListVo;
import com.ruoyi.by.health.domain.vo.LotteryOutcomeVo;
import com.ruoyi.by.health.service.IByhealthEmpPrizeService;
import com.ruoyi.by.health.service.IByhealthEmployeeService;
import com.ruoyi.by.health.service.IByhealthLotteryOutcomeService;
import com.ruoyi.by.health.service.IByhealthPrizeService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 员工信息管理
 *
 * @author zhangjq
 */
@Slf4j
@Api(value = "抽奖接口", tags = "抽奖接口")
@RestController
@RequestMapping("/byhealth/lottery")
public class ByhealthLotteryController extends BaseController {
    @Resource
    public RedisTemplate<String, Object> redisTemplate;
    @Autowired
    public IByhealthEmployeeService employeeService;
    @Autowired
    public IByhealthPrizeService prizeService;
    @Autowired
    public IByhealthEmpPrizeService empPrizeService;
    @Autowired
    public IByhealthLotteryOutcomeService lotteryOutcomeService;

    /**
     * 获取获奖名单
     */
    @ApiOperation("获取获奖名单")
    @GetMapping("list")
    public TableDataInfo list(ByhealthLotteryOutcome lotteryOutcome) {
        startPage();
        List<ByhealthLotteryOutcome> list = lotteryOutcomeService.selectLotteryOutcomeList(lotteryOutcome);
        return getDataTable(list);

    }

    /**
     * 获取用户列表
     */
    @ApiOperation("抽奖2")
    @GetMapping("/lotteryTest")
    public AjaxResult lotteryTest(Long rulesId) throws CloneNotSupportedException, InterruptedException {
        return empPrizeService.lotteryPrize(rulesId);
    }

    /**
     * 获取用户列表
     */
    @ApiOperation("重置抽奖")
    @GetMapping("/clearLottery")
    public AjaxResult clearLottery() {
        redisTemplate.delete("emp_prize_logs");
        return AjaxResult.success();
    }

    /**
     * 获取用户列表
     */
    @ApiOperation("重置投票")
    @GetMapping("/clearVote")
    public AjaxResult clearVote() {
        redisTemplate.delete("emp_bill_vote");
        return AjaxResult.success();
    }

    /**
     * 获取用户列表
     */
    @ApiOperation("抽奖")
    @GetMapping
    public AjaxResult lottery() {
        List<ByhealthPrize> prizes = prizeService.getByhealthPrizeList();

        List<ByhealthEmployee> bigEmployees = employeeService.selectRandomEmployeeList(8);
        List<ByhealthEmployee> employees = employeeService.selectRandomEmployeeList(900);
        List<LotteryOutcomeVo> smallLotteryOutcome = new ArrayList<>();
        List<LotteryOutcomeVo> bigLotteryOutcome = new ArrayList<>();

        for (ByhealthEmployee emp : bigEmployees) {
            Integer random = getRndInteger(0, prizes.size() - 1);
            LotteryOutcomeVo outcomeVo = new LotteryOutcomeVo();
            outcomeVo.setEmpCode(emp.getEmpCode());
            outcomeVo.setEmpName(emp.getEmpName());
            outcomeVo.setDeptName(emp.getDeptName());
            outcomeVo.setPrizeName(prizes.get(random).getPrizeName());
            outcomeVo.setPrizeType(prizes.get(random).getPrizeType());
            bigLotteryOutcome.add(outcomeVo);
        }

        for (ByhealthEmployee emp : employees) {
            Integer random = getRndInteger(0, prizes.size() - 1);
            LotteryOutcomeVo outcomeVo = new LotteryOutcomeVo();
            outcomeVo.setEmpCode(emp.getEmpCode());
            outcomeVo.setEmpName(emp.getEmpName());
            outcomeVo.setDeptName(emp.getDeptName());
            outcomeVo.setPrizeName(prizes.get(random).getPrizeName());
            outcomeVo.setPrizeType(prizes.get(random).getPrizeType());
            smallLotteryOutcome.add(outcomeVo);
        }
        LotteryOutcomeListVo oul = new LotteryOutcomeListVo();
        oul.setBigLotteryOutcome(bigLotteryOutcome);
        oul.setSmallLotteryOutcome(smallLotteryOutcome);
        return AjaxResult.success(oul);

    }


    private Integer getRndInteger(int x, int y) {
        int num = -1;
        //说明：两个数在合法范围内，并不限制输入的数哪个更大一些
        if (x < 0 || y < 0) {
            return num;
        } else {
            int max = Math.max(x, y);
            int min = Math.min(x, y);
            //求差
            int mid = max - min;
            //产生随机数
            num = (int) (Math.random() * (mid + 1)) + min;
        }
        return num;
    }

}
